package edu.example;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;



public class selectStudentsInViewTable extends JDialog
{
	//No manipulate, simple pass through
	private Vector<String> fields = new Vector<String>();
	private ApplicationManagerCloud am;
	private UserManager um;
	
	private JTable studentsTable;
	private JScrollPane scrollPane;
	private JPanel mainPanel;
	private JButton backArrowButton;
	private JButton nextButton;
	private TreeMap<String, Application> data = new TreeMap<String, Application>();
	//private Vector names = new Vector();
	private Vector<String> attributes = new Vector<String>();
	private Vector rowData = new Vector();
	
	private Application selectedApp;
	
	private Vector<Application> selectedApps = new Vector<Application>();
	
	private NextHandler nextHandler;
	private BackHandler backHandler;
	private SelectionListener tableSelectionListener;
	
	
	public selectStudentsInViewTable(ApplicationManagerCloud initAm, UserManager initUm, 
			TreeMap<String, Application> initData, Vector<String> initfields)
	{
		this.setTitle("Select Students");
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		
		am = initAm;
		um = initUm;
		fields = initfields;
		data = initData;
		initData();
		layoutGUI();
	}
	
	public void initData()
	{
		attributes.add("");
		attributes.add(" Name ");
		attributes.add("");
		attributes.add(" Name ");
		attributes.add("");
		attributes.add(" Name ");
		if(!data.isEmpty())
		{
			Collection<Application> temp = data.values();
			Iterator<Application> it = temp.iterator();
			
			int counter = 0;
			
			while(it.hasNext())
			{
				Vector names = new Vector();
				Application tempApp= it.next();
				Application tempApp1= it.next();
				Application tempApp2= it.next();
				
				names.add(false);
				names.add(tempApp.getApplicantLastName()+" "+tempApp.getApplicantFirstName()+"("+tempApp.getEmailAddress()+")");
				names.add(false);
				names.add(tempApp1.getApplicantLastName()+" "+tempApp1.getApplicantFirstName()+"("+tempApp1.getEmailAddress()+")");
				names.add(false);
				names.add(tempApp2.getApplicantLastName()+" "+tempApp2.getApplicantFirstName()+"("+tempApp2.getEmailAddress()+")");
				
				rowData.add(names);
				counter++;
			}
		    
		}
		else JOptionPane.showMessageDialog(this, 
				"Error: There are no students existing.");
	}
	
	public void layoutGUI()
	{
		studentsTable = new JTable(rowData,attributes)
		{
			//Disable editing in the table
		    public boolean isCellEditable(int rowIndex, int ColIndex) 
		    {
		    	if(ColIndex%2 != 0)
		    	{
		    		return false;
		    	}
		    	return true;
		    }
		    public Class<?> getColumnClass(int columnIndex) 
		    {
		    	if (getColumnName(columnIndex).isEmpty())
		            return Boolean.class;
		        return super.getColumnClass(columnIndex);
		    }
		};


		
		studentsTable.setPreferredScrollableViewportSize(new Dimension(720,300));
		studentsTable.setCellSelectionEnabled(true);
		studentsTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		scrollPane = new JScrollPane(studentsTable);
		mainPanel = new JPanel();
		mainPanel.add(scrollPane);
		this.add(mainPanel);
		
		ImageIcon icon = new ImageIcon("record_back.png");
		backArrowButton = new JButton(icon);
		nextButton = new JButton("Next");
		
		this.setLayout(new GridBagLayout());
		addComponent(backArrowButton,			this,	0,	0,	1,	1);
		addComponent(mainPanel,					this,	1,	1,	1,	1);
		addComponent(nextButton,				this,	2,	2,	1,	1);
		
		scrollPane.getViewport().setBackground(Color.WHITE);
		Container c = this.getContentPane();
		c.setBackground(Color.WHITE);
		mainPanel.setBackground(Color.WHITE);
		studentsTable.getTableHeader().setBackground(Color.WHITE);
		
		
		// size and locate the dialog at the center of the screen
		this.pack();
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
		nextHandler = new NextHandler();
		backHandler = new BackHandler();
		tableSelectionListener = new SelectionListener();
		
		backArrowButton.addActionListener(backHandler);
		nextButton.addActionListener(nextHandler);
		
		studentsTable.addMouseListener(tableSelectionListener);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	public static void main(String[] args)
	{
		File file1 = new File("AY(New).txt");
		File file2 = new File("AY-map.txt");
		File file3 = new File("OTS.txt");
		File file4 = new File("OTS-map.txt");
		ApplicationManagerCloud formatter = new ApplicationManagerCloud(null, file1, file2, file3, file4);
		UserManager um = new UserManager();
		um.login("chair", "abc");
		selectStudentsInViewTable temp = new selectStudentsInViewTable(formatter, um, formatter.getAllApps(), null);
		temp.setVisible(true);
	}
	
	class NextHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			for(int i = 0; i<selectedApps.size(); i++)
			{
				System.out.println(selectedApps.get(i).getEmailAddress());
			}
			viewAppTable temp = new viewAppTable(am, um, selectStudentsInViewTable.this, fields, selectedApps);
			temp.setVisible(true);
			selectStudentsInViewTable.this.setVisible(false);
		}
		
	}
	
	class BackHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			selectFieldsInViewTable temp  = new selectFieldsInViewTable(am, um);
			selectStudentsInViewTable.this.setVisible(false);
			temp.setVisible(true);
		}
		
	}
	
	class SelectionListener implements MouseListener
	{
		public void mouseClicked(MouseEvent e) 
		{
			int row = studentsTable.getSelectedRow();
			int col = studentsTable.getSelectedColumn();
			
			if(col%2 == 0)
			{
				boolean flag = (boolean) studentsTable.getValueAt(row, col);
				System.out.println(flag);
				
				if(flag)
				{
					String temp = (String)studentsTable.getValueAt(row, col+1);
					int beginIndex = temp.indexOf("(");
					int endIndex = temp.indexOf(")");
					String email = temp.substring(beginIndex+1, endIndex);
					selectedApp = data.get(email);
					if(!selectedApps.contains(selectedApp))
						selectedApps.add(selectedApp);
				}
				else
				{
					String temp = (String)studentsTable.getValueAt(row, col+1);
					int beginIndex = temp.indexOf("(");
					int endIndex = temp.indexOf(")");
					String email = temp.substring(beginIndex+1, endIndex);
					selectedApp = data.get(email);
					if(selectedApps.contains(selectedApp))
						selectedApps.remove(selectedApp);
					selectedApp = null;
				}
			}
			
			if(!selectedApps.isEmpty())
			{
				nextButton.setEnabled(true);
			}
			else
			{
				nextButton.setEnabled(false);
			}
		}

		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		
	}
}
